import { getLicenseInfo } from '@api/license'
import { getStorage, setStorage } from '@utils/auth'

export default {
  state: {
    noLicense: false,
    licenseInfo: {},
  },
  mutations: {
    // 获取license数据,仅保存学校信息
    setLicenseInfo(state, data) {
      state.licenseInfo = data
    },
    // 没有license数据
    setNoLicense(state, data) {
      state.noLicense = data
    }
  },
  actions: {
    // 判断是否配置license信息
    async getLicenseInfo({ commit }, params) {
      const userInfo = getStorage('userInfo')
      let newUserInfo = {}
      const [err, res] = await getLicenseInfo()
      if (err) {
        if (err.errmsg.includes('未获取到license文件')) {
          commit('setNoLicense', false)
        } else {
          commit('setNoLicense', true)
          Message.error(err.errmsg)
        }
        newUserInfo = { ...userInfo, schoolCode: null, schoolName: null }
        setStorage('userInfo', newUserInfo)
        commit('setLicenseInfo', {})
        return
      }
      commit('setNoLicense', true)
      const { schoolInfo } = res.data || []
      newUserInfo = { ...userInfo, schoolCode: schoolInfo.schoolCode, schoolName: schoolInfo.schoolName }
      setStorage('userInfo', newUserInfo)
      commit('setLicenseInfo', schoolInfo)
      return res.data || []
    }
  },
  getters: { }
}
